package cn.edu.ctbu.spring_demo.webapi;

import cn.edu.ctbu.spring_demo.core.PageUtils;
import cn.edu.ctbu.spring_demo.domain.Student;
import cn.edu.ctbu.spring_demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 读取全部内容
     * 读取的 URL：/webapi/student/list
     * @return List<Student> 一个学生信息列表
     */
    @GetMapping("/list")
    public List<Student> getAll(){

        return studentService.findAll();
    }

    /**
     * 分页，读取全部内容
     * 读取的 URL：/webapi/student/getbypage
     * @return PageUtils 以分页的方式读取学生信息
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                               @RequestParam(value = "size", defaultValue = "10") Integer size,
                               @RequestParam(value = "name", defaultValue = "") String name){

        //按id倒序查询
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student> studentPage;

        if(StringUtils.isEmpty(name)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentService.findAll(pageable);
        }else{
            Student student = new Student();
            student.setName(name);

            Pageable pageable = PageRequest.of(page,size,sort);
            ExampleMatcher matcher = ExampleMatcher.matching().
                    withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询，即%name%
            Example<Student> studentExample = Example.of(student,matcher);

            studentPage = studentService.findAll(studentExample, pageable);
        }

        PageUtils pageUtils = new PageUtils(studentPage.getContent(), studentPage.getTotalElements());

        return pageUtils;
    }

    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){

        Student student = studentService.findById(id);
        student.setPassword("");

        return student;
    }

    //查询学生信息
    @GetMapping("/search/{name}")
    public List<Student> getByName(@PathVariable String name) {
        List<Student> students = studentService.findByName(name);

        return students;
    }

    /**
     * 新增学生信息的方法：/webapi/student/insert
     * @param student Student类的对象
     * @return studentService.insert(student) Student类的对象
     */
    @PostMapping("/insert")
    public Student insert(Student student){

        return studentService.insert(student);
    }

    /**
     * 更新学生信息的方法：/webapi/student/update
     * @param student Student类的对象
     * @return studentService.insert(student) Student类的对象
     */
    @PutMapping("/update")
    public Student update(Student student){

        Student student1 = studentService.findById(student.getId());

        if(StringUtils.isEmpty(student.getPassword())) {
            //如果没有新的密码，就用旧的密码
            student.setPassword(student1.getPassword());
        }

        return studentService.update(student);
    }

    /**
     * 删除学生信息的方法：/webapi/student/update
     * @param id Student类的对象的id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        studentService.deleteById(id);
    }
}
